using System.Collections.Generic;
using Firestarter.DB.Schema.BO;
using Firestarter.DM_DB.Logic;
using FireStarterModeller.BO;
using FireStarterModeller.Test;
using NUnit.Framework;

namespace Firestarter.DB_DM.Logic.Test
{
    [TestFixture]
    public class TestDBFactory_CreateDBForeignKey : TestBase
    {
        [Test]
        public void Test_CreateDBForeignKey()
        {
            //---------------Set up test pack-------------------
            DMClass @class = TestUtilsDMClass.CreateUnsavedValidDMClass();
            DMProperty dmProperty = TestUtilsShared.CreateUnsavedValidDMProperty(@class);
            DMClass @class2 = TestUtilsDMClass.CreateSavedDMClassWithOID(0);
            DMRelationship relationship = @class.RelatedRelationships.CreateBusinessObject();
            relationship.RelatedClass = @class2;
            relationship.RelationshipName = TestUtilsShared.GetRandomString();
            DMRelationshipProperty relProp = relationship.RelationshipProperties.CreateBusinessObject();
            relProp.OwnerProperty = dmProperty;
            DMProperty relatedProperty = @class2.Properties[0];
            relProp.RelatedProperty = relatedProperty;
            DBFactory factory = TestDBFactory.CreateDBFactory();
            //---------------Execute Test ----------------------
            DBTable table = factory.CreateDBTable(@class);
            DBTable relatedTable = factory.CreateDBTable(@class2);
            DBForeignKey foreignKey = factory.CreateDBForeignKey(relationship, table, relatedTable);
            ////---------------Test Result -----------------------
            Assert.AreEqual(1, foreignKey.ForeignKeyColumns.Count);
            Assert.AreEqual(dmProperty.MappedDBColumnName, foreignKey.ForeignKeyColumns[0].ParentColumnName);
            Assert.AreEqual(relatedProperty.MappedDBColumnName, foreignKey.ForeignKeyColumns[0].RelatedColumnName);
            Assert.AreEqual(table, foreignKey.ParentTable);
            Assert.AreEqual(relatedTable, foreignKey.RelatedTable);
        }
    }
}